package com.jxs.android.sxyw;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView.ScaleType;

import com.jxs.android.sxyw.image.ChainImageProcessor;
import com.jxs.android.sxyw.image.ImageProcessor;
import com.jxs.android.sxyw.image.MaskImageProcessor;
import com.jxs.android.sxyw.weiget.AsyncImageView;
import com.jxs.android.sxyw.weiget.ScaleImageProcessor;
import com.jxs.android.sxyw.weiget.TopBar;

/**
 * @Des 选择图片
 * @author Jiangxs
 * 
 */
public class ViewPicGrid extends AbstractActivity implements OnItemClickListener {

	private GridView appGridView = null;
	private Context context;
	private ArrayList<String> fileUrls = new ArrayList<String>();
	private PicAdpter picAdpter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.view_pic_grid);

		context = this;
		appGridView = (GridView) findViewById(R.id.main_app_gridview);
		if (null != getIntent() && null != getIntent().getExtras()) {
			fileUrls = getIntent().getExtras().getStringArrayList("fileLists");
		}
		picAdpter = new PicAdpter(context);
		appGridView.setAdapter(picAdpter);
		appGridView.setOnItemClickListener(this);
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.hindBtn();
		topBar.setTitle("附件照片");
		initTopBarReturn(topBar);
	}

	/**
	 * @Des 点击中间的图片的时候处理事件
	 */

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(this, ViewPicWeb.class);
		Bundle bundle = new Bundle();
		bundle.putString("url", fileUrls.get(position));
		intent.putExtras(bundle);
		startActivity(intent);
	}

	private class PicAdpter extends BaseAdapter {

		private LayoutInflater inflater = null;
		private ImageProcessor mImageProcessor;

		public PicAdpter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			prepareImageProcessor(context);
		}

		private void prepareImageProcessor(Context context) {

			final int thumbnailSize = context.getResources().getDimensionPixelSize(R.dimen.thumbnail_size);
			final int thumbnailRadius = context.getResources().getDimensionPixelSize(R.dimen.thumbnail_radius);

			if (Math.random() >= 0.5f) {
				// @formatter:off
				mImageProcessor = new ChainImageProcessor(new ScaleImageProcessor(thumbnailSize, thumbnailSize, ScaleType.FIT_XY), new MaskImageProcessor(thumbnailRadius));
				// @formatter:on
			} else {

				Path path = new Path();
				path.moveTo(thumbnailRadius, 0);

				path.lineTo(thumbnailSize - thumbnailRadius, 0);
				path.lineTo(thumbnailSize, thumbnailRadius);
				path.lineTo(thumbnailSize, thumbnailSize - thumbnailRadius);
				path.lineTo(thumbnailSize - thumbnailRadius, thumbnailSize);
				path.lineTo(thumbnailRadius, thumbnailSize);
				path.lineTo(0, thumbnailSize - thumbnailRadius);
				path.lineTo(0, thumbnailRadius);

				path.close();

				Bitmap mask = Bitmap.createBitmap(thumbnailSize, thumbnailSize, Config.ARGB_8888);
				Canvas canvas = new Canvas(mask);

				Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
				paint.setStyle(Style.FILL_AND_STROKE);
				paint.setColor(Color.RED);

				canvas.drawPath(path, paint);

				// @formatter:off
				mImageProcessor = new ChainImageProcessor(new ScaleImageProcessor(thumbnailSize, thumbnailSize, ScaleType.FIT_XY), new MaskImageProcessor(mask));
				// @formatter:on
			}
		}

		@Override
		public int getCount() {
			return fileUrls.size();
		}

		@Override
		public String getItem(int position) {
			return fileUrls.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.image_item_view, parent, false);
				holder = new ViewHolder();
				holder.imageView = (AsyncImageView) convertView.findViewById(R.id.async_image);
				holder.imageView.setImageProcessor(mImageProcessor);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.imageView.setUrl(fileUrls.get(position));

			return convertView;

		}

		class ViewHolder {
			public AsyncImageView imageView;
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
	}

}